#
# fusion.http.server
#
# Copyright 2007 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Ken Rimey <rimey@hiit.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

'''
Generic Web Interface

The variable "urls" in this module specifies how URLs matching various
regular expressions are dispatched to resource classes such as Value
or Folder.
'''

# XXX Add remaining methods as needed, starting with put().  Support
# X-HTTP-Method-Override header if necessary.

from fusion.http.http_server import init_http, serve_http

main_urls = (
    '/profiles?/default', 'fusion.http.Profile.Profile',
    '/profiles?/([0-9a-f]{40})', 'fusion.http.Profile.Profile',
    '/feed/([0-9a-f]{40})', 'fusion.feed.Feed.Feed',
    '/feed/([0-9a-f]{40})/(.+)', 'fusion.feed.Feed.Feed',
    '/tracker/([0-9a-f]{40})/(.*)', 'fusion.tracker.Tracker.Tracker',
    '/island/([0-9a-f]{40})/(.*)', 'fusion.tracker.Island.Island',
    '/coast/([0-9a-f]{40})/(.*)', 'fusion.tracker.Coast.Coast',
    '/collections?/([0-9a-f]{40})', 'fusion.http.Value.Value',
    '/collections?/([0-9a-f]{40})/', 'fusion.http.Folder.Folder',
    '/collections?/([0-9a-f]{40})/(.+)/', 'fusion.http.Folder.Folder',
    '/collections?/([0-9a-f]{40})/(.+)', 'fusion.http.Value.Value',
    '/info', 'fusion.http.Info.Info',
    '/disco', 'fusion.http.Disco.Disco',
    '/(.*)', 'fusion.http.Default.Default',
    )

admin_urls = (
    '/profiles/?', 'fusion.http.Profiles.Profiles',
    '/collections/?', 'fusion.http.Collections.Collections',
    '/(.*)', 'fusion.http.Admin.Admin',
    )

MAIN = None
ADMIN = None

def serve_main(host='127.0.0.1', port=8080):
    global MAIN

    if host.startswith('127.'):
        MAIN = 'http://%s:%d' % ('localhost', port)
    else:
        MAIN = 'http://%s:%d' % (host, port)

    serve_http(main_urls, host, port)

def serve_admin(host='127.0.0.1', port=8081):
    global ADMIN

    if host.startswith('127.'):
        ADMIN = 'http://%s:%d' % ('localhost', port)
    else:
        ADMIN = 'http://%s:%d' % (host, port)

    serve_http(admin_urls, host, port)
